// pages/function/resale/resale.js
import {
  behavior as computedBehavior
} from 'miniprogram-computed';
Page({
  behaviors: [computedBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    heightConfig: 0,
    widthConfig: 0,
    hideHeader: true,
    searchValue: "",
    searchDisable: false,
    swiperMessage: [],
    typesList: [],
    goodsList: [],
    goodsList_page: 1,
    goodsList_end: false,
    // nav的加载
    navloading: false
  },

  watch: {
    "hideHeader": function (newValue) {
      if (!newValue) return;
      this.setData({
        goodsList_page: 1,
        goodsList_end: false
      })
      this.getGoodsList(1, 7)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.setData({
      navloading: true,
      navTop: getApp().globalData.navTop,
      heightConfig: getApp().globalData.windowHeight,
      widthConfig: getApp().globalData.windowWidth,
    })
    let that = this;
    /**
     * 获取轮播图
     */
    getApp().requestWithoutToken.get('/api/getSwiperMessage').then(result => {
      that.setData({
        navloading: false,
        swiperMessage: result.data,
        "loadShow[1]": 0
      })
    });
    this.getTypes();
    this.getGoodsList(1, 7);
  },

  getLocation() {
    // 暂无权限 先注释
    // wx.getFuzzyLocation({
    //   type: 'gcj02',
    //   success: (result) => {
    //     console.log(result);
    //   },
    //   fail: (res) => {
    //     console.log(res);
    //   },
    // })
  },
  // 搜索
  toSearchGoods() {
    if (!this.data.searchValue) return wx.showToast({
      title: '请输入内容后搜索',
      icon: 'error'
    })
    this.setData({
      searchDisable: true
    })
    wx.navigateTo({
      url: './types/types?key=' + this.data.searchValue,
      success: () => {
        this.setData({
          searchDisable: false,
          searchValue: ""
        })
      }
    })
  },

  getTypes() {
    getApp().requestWithToken.get("/type/getList").then(res => {
      if (res.code != 200) return;
      this.setData({
        typesList: res.data
      })
    })
  },

  getGoodsList(index, size = 5) {
    console.log("page: ", index)
    let that = this;
    /**
     * 获取最新的几篇文章
     */
    if (this.data.goodsList_end) {
      return;
    }

    getApp().requestWithToken.get('/sechandGoods/getGoodsPage', {
      data: {
        pageNo: index,
        pageSize: size
      }
    }).then(result => {
      const {
        list = []
      } = result.data
      list.forEach(item => {
        item.goodsImage = that.getGoodsImg(item.goodsPhotos)
      })
      if (index == 1) {
        that.setData({
          goodsList: list
        })
      } else {
        that.setData({
          goodsList: that.data.goodsList.concat(list),
          goodsList_page: result.data.pageNum
        })
      }
      if (!result.data.hasNextPage) {
        that.setData({
          goodsList_end: true
        })
      }
    });
  },

  getGoodsImg(e) {
    if (!e) return;
    let url = getApp().globalData.imageUrl + e
    return url
  },

  reflash() {
    this.setData({
      hideHeader: false
    })
    setTimeout(() => {
      this.setData({
        hideHeader: true
      })
    }, 1000);
  },

  nextGoodsPage() {
    wx.showLoading({
      title: '加载中',
    })
    this.getGoodsList(this.data.goodsList_page + 1)
    wx.hideLoading()
  },

  // 跳转到发布
  navPublish() {
    wx.navigateTo({
      url: '/pages/function/resale/publish/publish',
    })
  },
  // 跳转到分类
  navType(e) {
    wx.navigateTo({
      url: `/pages/function/resale/category/category?id=${e.currentTarget.dataset.id}&name=${e.currentTarget.dataset.name}`,
    })
  },

  // 跳转到商品详情
  navGoodsDetail(e) {
    wx.navigateTo({
      url: `/pages/function/resale/goodsDeatil/goodsDetail?id=${e.currentTarget.dataset.id}`,
    })
  },
  // 跳转到我的商品
  navMyGoods() {
    wx.navigateTo({
      url: '/pages/function/resale/myGoods/myGoods',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})